package com.cpos.pay.payserver.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cpos.cposcommon.util.CposResult;
import com.cpos.pay.payserver.entity.OrderInfo;
import com.cpos.pay.payserver.entity.PayInfo;
import com.cpos.pay.payserver.entity.UserInfo;
import com.cpos.pay.payserver.service.OrderService;
import com.cpos.pay.payserver.service.PayService;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @auth Leiwenlong
 * @since 2020/12/9
 */
@Slf4j
@RequestMapping("/pay")
public class PayController {

    final PayService payService;
    final OrderService orderService;
    final Map<Object, Object> objectObjectMap  = new HashMap<>();

    public PayController(PayService payService,OrderService orderService){
        this.payService = payService;
        this.orderService = orderService;
    }


    /*
     *   支付宝 回调接口
     * */
    @PostMapping("/alipay/noNotify")
    public CposResult AlipayInNoNotify(@RequestBody PayInfo payInfo, UserInfo userInfo){
      return new CposResult(500,"","系统报错",new Date());
    }

    /*
     *   微信 回调接口
     * */
    @PostMapping("/wxpay/noNotify")
    public CposResult WxpayInNoNotify(@RequestBody PayInfo payInfo, UserInfo userInfo){
        return new CposResult(500,"","系统报错",new Date());
    }


    /*
    *   支付接口
    * */
    @PostMapping("/pay")
    public CposResult pay(@RequestBody PayInfo payInfo, UserInfo userInfo){

        if(ObjectUtils.isEmpty(payInfo)){
            return  new CposResult(500,"","支付参数不完善",new Date());
        }
        if(ObjectUtils.isEmpty(userInfo)){
            return  new CposResult(500,"","用户信息不完善",new Date());
        }

        //用户信息健全
        String userId = userInfo.getUserId();
        if(userId.equals("223")){
            return  new CposResult(500,"","用户不存在",new Date());
        }

        //订单号
        String orderId = payInfo.getOrderId();
        if(StringUtil.isNullOrEmpty(orderId)){
            return  new CposResult(500,"","订单号为空",new Date());
        }
        OrderInfo order = orderService.getOrder(orderId);
        if(ObjectUtils.isEmpty(order)){
            return  new CposResult(500,"","订单不存在",new Date());
        }

        if(ObjectUtil.isNotEmpty(payInfo.getPayType())){
            switch (payInfo.getPayType()){
                default:
                    return new CposResult(500,"","支付异常",new Date());
                case 1:
                    Map<Object, Object> objectObjectMap = payService.BuyAliPayInApp(order);
                    break;
                case 2:
                    objectObjectMap = payService.BuyAliPayInH5(order);
                    Object o = objectObjectMap.get("order");
                    if(o.equals("xx1")){
                        return new CposResult(200,"","支付成功",new Date());
                    }
                    break;
                case 3:
                    objectObjectMap = payService.BuyWxPayInApp(order);
                    break;
                case 4:
                    objectObjectMap = payService.BuyWxPayInH5(order);
                    break;
                case 5:
                    objectObjectMap = payService.BuyWxPayApplets(order);
                    break;
                case 6:
                    objectObjectMap = payService.BuyWxPayPublicNum(order);
                    break;
            }
        }

        return new CposResult(500,"","系统报错",new Date());
    }

}
